package com.tx.studentManager.service;

import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tx.common.format.util.DateUtil;
import com.tx.studentManager.dao.StudentDao;
import com.tx.studentManager.model.Student;
import com.tx.studentManager.qo.StudentQO;

@Transactional(rollbackFor = RuntimeException.class)
@Service
public class StudentService extends BaseService<Student, Integer, StudentDao, StudentQO>{
	@Autowired
	private StudentDao studentDao;

	@Override
	protected StudentDao getDao() {
		return studentDao;
	}

	@Override
	public Student insert(Student model) {
		if (model.getCreateDate()==null) {
			model.setCreateDate(new Date());
		}
		return super.insert(model);
	}
/**
 * 
* @描述 获取下载的文件 
* @param studentList
* @return
 */
	@SuppressWarnings("deprecation")
	public HSSFWorkbook export(List<Student> studentList) {
	        HSSFWorkbook wb = new HSSFWorkbook();   
	        HSSFSheet sheet = wb.createSheet("学生表");    
	        HSSFRow row = sheet.createRow((int) 0);    
	        HSSFCellStyle style = wb.createCellStyle();    
	        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);    
	        String excelHeader[]=new String[]{"姓名","性别","年龄","班级","地址","电话","邮箱","创建日期"};
	        for (int i = 0; i < excelHeader.length; i++) {    
	            HSSFCell cell = row.createCell((short) i);    
	            cell.setCellValue(excelHeader[i]);    
	            cell.setCellStyle(style);    
	        }    
	    
	        for (int i = 0; i < studentList.size(); i++) {    
	            row = sheet.createRow(i + 1);    
	            Student student = studentList.get(i);    
	            row.createCell((short) 0).setCellValue(student.getName());    
	            row.createCell((short) 1).setCellValue(student.getSex());    
	            row.createCell((short) 2).setCellValue(student.getAge());    
	            row.createCell((short) 3).setCellValue(student.getStudentClass()==null?"":student.getStudentClass().getClassName());    
	            row.createCell((short) 4).setCellValue(student.getAddress());    
	            row.createCell((short) 5).setCellValue(student.getStudentInfo().getPhone());    
	            row.createCell((short) 6).setCellValue(student.getStudentInfo().getEmail());    
	            row.createCell((short) 7).setCellValue(DateUtil.simpleFormat_24(student.getCreateDate()));    
	        }    
	        for (int i = 0; i < excelHeader.length; i++) {
	        	sheet.autoSizeColumn((short) i);    
			}
	        return wb;    
	    }    
	
	
}
